<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    :ok-button-props="{ props: { disabled: disableSubmit } }"
    cancelText="关闭"
  >
    <template slot="footer">
      <a-button @click="handleCancel">关闭</a-button>
      <a-button v-if="!disableSubmit" type="primary" @click="handleOk" :loading="buttonok">确定</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-col :span="24">
            <a-form-item label="员工姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-search-select-tag
                :disabled="disableSubmit"
                v-decorator="['partyId', validatorRules.partyId]"
                dict="Person,name,partyId,typeId = EMPLOYEE"
                @change="autoValue"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="所属BU" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-category-select
                :disabled="true"
                v-decorator="['buId', validatorRules.buId]"
                pcode="{table:'HrDepartment',txt:'name',key:'departmentId',idField:'id',pidField:'parentId',hasChildField:'hasChild', condition:' parentId = 0'}"
                placeholder="请选择所属部门"
                back="title"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                :disabled="true"
                v-decorator="['positionName', validatorRules.positionName]"
                placeholder="请输入岗位"
              ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="职级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="true"
                type="list"
                v-decorator="['positionRank', validatorRules.positionRank]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = POSITION_RANK"
                placeholder="请选择职级"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入职日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="true"
                placeholder="请选择入职日期"
                v-decorator="['beginDate', validatorRules.beginDate]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="期望离职日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date
                :disabled="disableSubmit"
                placeholder="请选择期望离职日期"
                v-decorator="['estimateResignDate', validatorRules.estimateResignDate]"
                :trigger-change="true"
                style="width: 100%"
              />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="离职原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag
                :disabled="disableSubmit"
                type="list"
                v-decorator="['content', validatorRules.content]"
                :trigger-change="true"
                dictCode="DataDictionary,description,dictionaryCode, dictionaryTypeId = RESIGN_REASON"
                placeholder="请选择离职原因"
              />
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
        <a-form-item label="离职内容" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea :disabled="disableSubmit" v-decorator="['content', validatorRules.content]" rows="4" placeholder="请输入离职内容"/>
        </a-form-item>
</a-col> -->

          <a-col :span="24">
            <a-form-item label="附件" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-upload
                :disabled="disableSubmit"
                :isMultiple="false"
                v-decorator="['uploadPath']"
                :trigger-change="true"
              ></j-upload>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-textarea :disabled="disableSubmit" v-decorator="['remark']" rows="4" placeholder="请输入备注" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from '@/api/manage'
import pick from 'lodash.pick'
import { validateDuplicateValue } from '@/utils/util'
import JDate from '@/components/jeecg/JDate'
import JUpload from '@/components/jeecg/JUpload'
import JDictSelectTag from '@/components/dict/JDictSelectTag'
import JSearchSelectTag from '@/components/dict/JSearchSelectTag'
import JCategorySelect from '@/components/jeecg/JCategorySelect'

export default {
  name: 'HrResignModal',
  components: {
    JDate,
    JUpload,
    JDictSelectTag,
    JSearchSelectTag,
    JCategorySelect,
  },
  data() {
    return {
      form: this.$form.createForm(this),
      title: '操作',
      width: 800,
      visible: false,
      buttonok: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      disableSubmit: false,
      validatorRules: {
        partyId: {
          rules: [{ required: true, message: '请选择员工!' }],
        },
        buId: {
          rules: [{ required: true, message: '请选择BU!' }],
        },
        positionName: {
          rules: [{ required: true, message: '请输入岗位!' }],
        },
        positionRank: {
          rules: [{ required: true, message: '请选择职级!' }],
        },
        resignReason: {
          rules: [{ required: true, message: '请选择离职原因!' }],
        },
        content: {
          rules: [{ required: true, message: '请选择离职原因!' }],
        },
        beginDate: {
          rules: [{ required: true, message: '请选择入职日期!' }],
        },
        estimateResignDate: {
          rules: [{ required: true, message: '请选择期望离职日期!' }],
        },
        uploadPath: {
          rules: [{ required: true, message: '请输入附件!' }],
        },
        statusId: {
          rules: [{ required: true, message: '请输入状态!' }],
        },
      },
      url: {
        add: '/online/api/createHrResign',
        edit: '/online/api/updateHrResign',
        get: '/online/api/getHrResign',
        getHrEmployee: '/online/api/getHrEmployee',
      },
    }
  },
  created() {},
  methods: {
    add() {
      this.edit({})
    },
    edit(record) {
      this.form.resetFields()
      this.visible = true
      this.model = {}
      if (record.id) {
        httpAction(this.url.get, { id: record.id }, 'post').then((res) => {
          if (res.success) {
            this.model = Object.assign({}, res.result)
            this.$nextTick(() => {
              this.form.setFieldsValue(
                pick(
                  this.model,
                  'partyId',
                  'buId',
                  'positionName',
                  'positionRank',
                  'resignReason',
                  'content',
                  'estimateResignDate',
                  'beginDate',
                  'resignDate',
                  'uploadPath',
                  'statusId',
                  'remark',
                  'createBy',
                  'createDate'
                )
              )
            })
          }
        })
      }
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleOk() {
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          that.buttonok = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          let formData = Object.assign(this.model, values)
          console.log('表单提交数据', formData)
          httpAction(httpurl, formData, method)
            .then((res) => {
              if (res.success) {
                that.$message.success('提交成功!')
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
              setTimeout(() => {
                that.buttonok = false
              }, 500)
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        }
      })
    },
    handleCancel() {
      this.close()
    },
    popupCallback(row) {
      this.form.setFieldsValue(
        pick(
          row,
          'partyId',
          'buId',
          'positionName',
          'positionRank',
          'resignReason',
          'content',
          'estimateResignDate',
          'beginDate',
          'resignDate',
          'uploadPath',
          'statusId',
          'remark',
          'createBy',
          'createDate'
        )
      )
    },
    autoValue(e) {
      httpAction(this.url.getHrEmployee, { partyId: e }, 'post').then((res) => {
        let model = Object.assign({}, res.result)
        model.buId = res.result.departmentId
        this.$nextTick(() => {
          this.form.setFieldsValue(
            pick(
              model,
              'buId',
              'positionName',
              'positionRank',
              'beginDate',
            )
          )
        })
      })
    },
  },
}
</script>
